import Vue from 'vue'

const funs = {
	scanQrcode() {
		let msg = null
		let user = uni.getStorageSync('userInfo').用户信息 || uni.getStorageSync('userInfo')
		// 只允许通过相机扫码
		uni.scanCode({
			onlyFromCamera: true,
			success: (res) => {
				console.log(res)
				if (res.scanType == 'QR_CODE') {
					try {
						// console.log(JSON.parse(res.result))
						let index = res.result.indexOf('{')
						// console.log(index)
						let obj = JSON.parse(res.result.slice(index))
						// let obj = JSON.parse(res.result.trim())
						console.log('条码内容：', obj);
						if(obj.streetId !== user.所属街道id) {
							msg = '当前二维码与用户所属街道不匹配'
							uni.showModal({
								title: '提示',
								content: msg,
								showCancel: false,
								success: function(res) {
							
									uni.switchTab({
										url: "../index/index"
									})
								},
							})
							return
						}
						if (obj.qrcode == 'fireCheckGrid') {
							//检查网格员所属网格id和二维码所属网格id相等，即网格员是否有权限
							if (obj.type == "facility") {
								// 跳转设施页面
								uni.redirectTo({
									url: "/fireGridCheck/installtionInfo/installtionInfo?id=" + obj.id
								})
	
	
							} else if (obj.type == "checkPoint") {
								
								// 跳转检查点页面
								uni.redirectTo({
									url: "/fireGridCheck/checkPoint/checkPoint?id=" + obj.id + "&gridId=" + obj.gridId
								})
	
								// uni.navigateTo({
								// 	url: "/pages/checkPoint/checkPoint"
								// })
							} else if (obj.type == 'building') {
								console.log('触发了')
								uni.redirectTo({
									url: '/fireGridCheck/buildScanInfo/buildScanInfo?id=' + obj.id + '&checkId=' + obj.checkPointId
								})
							}
	
						} else {
							msg = "非法二维码"
						}
					} catch (e) {
						console.log(e)
						msg = "非法二维码"
					}
	
				} else {
					msg = "请扫描二维码"
				}
				if (msg) {
					uni.showModal({
						title: '提示',
						content: msg,
						showCancel: false,
						success: function(res) {
	
							uni.switchTab({
								url: "../index/index"
							})
						},
					})
	
				}
	
				// console.log('条码类型：' + res.scanType);
				// console.log('条码内容：' + res.result);
			},
			fail: function(err) {
				// uni.showModal({
				//     title: '提示',
				//     content: '请扫描正确的二维码',
				// 	showCancel: false,
				//     success: function (res) {
				//         if (res.confirm) {
				//             console.log('用户点击确定');
				//         } else if (res.cancel) {
				//             console.log('用户点击取消');
				//         }
				//     }
				// });
			}
		});
	},
	formatCountDown(data, timeLimit) {
		let time = data.工单计时
		let timeType = data.工单计时类型
		let day = parseInt(time / 86400)
		let hour = parseInt((time % 86400) / 3600)
		let minute = parseInt((time % 86400) % 3600 / 60)
		let second = parseInt((time % 86400) % 3600 % 60)
	
		if (!day && hour && minute && second && timeType === 1) {
			return hour + '小时' + minute + '分' + second + '秒'
		} else if (day && !hour && minute && second && timeType === 1) {
			return day + '天' + minute + '分' + second + '秒'
		} else if (day && hour && !minute && second && timeType === 1) {
			return day + '天' + hour + '小时' + second + '秒'
		} else if (day && hour && minute && !second && timeType === 1) {
			return day + '天' + hour + '小时' + minute + '分'
		} else if (!day && !hour && minute && second && timeType === 1) {
			return minute + '分' + second + '秒'
		} else if (!day && hour && !minute && second && timeType === 1) {
			return hour + '时' + second + '秒'
		} else if (!day && hour && minute && !second && timeType === 1) {
			return hour + '时' + minute + '分'
		} else if (day && !hour && !minute && second) {
			return day + '天' + second + '秒'
		} else if (day && !hour && minute && !second && timeType === 1) {
			return day + '天' + minute + '分'
		} else if (day && hour && !minute && !second && timeType === 1) {
			return day + '天' + hour + '时'
		} else if (!day && !hour && !minute && second && timeType === 1) {
			return second + '秒'
		} else if (day && !hour && !minute && !second && timeType === 1) {
			return day + '天'
		} else if (!day && hour && !minute && !second && timeType === 1) {
			return hour + '时'
		} else if (!day && !hour && minute && !second && timeType === 1) {
			return minute + '分'
		} else if (timeType === 2 && timeLimit) {
			return '已超时'
		} else {
			return ''
		}
	
	},
	secondToHour(second) {
	    let dd, hh, mm, ss;
	    second = typeof second === "string" ? parseInt(second) : second;
	    if (!second || second < 0) {
	      return 0;
	    }
	    // 天
	    dd = (second / (24 * 3600)) | 0;
	    second = Math.round(second) - dd * 24 * 3600;
	    // 小时
	    hh = (second / 3600) | 0;
	    second = Math.round(second) - hh * 3600;
	    // 分
	    mm = (second / 60) | 0;
	    // 秒
	    ss = Math.round(second) - mm * 60;
	    if (Math.round(dd) < 10) {
	      dd = dd > 0 ? "0" + dd : "00";
	    }
	    if (Math.round(hh) < 10) {
	      hh = "0" + hh;
	    }
	    if (Math.round(mm) < 10) {
	      mm = "0" + mm;
	    }
	    if (Math.round(ss) < 10) {
	      ss = "0" + ss;
	    }
	    let result = ss + "秒";
	    if (mm !== "00") {
	      result = mm + "分" + result;
	    }
	    if (hh !== "00") {
	      result = hh + "时" + result;
	    }
	    if (dd !== "00") {
	      result = dd + "天" + result;
	    }
	    return result;
	  },
	// 手机号码
	isCorrectMobile(mobile) {
		if (!mobile) {
			return false
		}
		if (!/^1[23456789]\d{9}$/.test(mobile.trim())) {
			return false;
		}
		return true;
	},
	// 身份证号码
	isCorrectPersonId(personId) {
		if (!personId) {
			return false
		}
		let p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if (!p.test(personId.trim())) {
			return false;
		}
		return true;
	},
	//生成唯一编码
	uuid() {
		let s = [];
		let hexDigits = "0123456789abcdef";
		for (let i = 0; i < 36; i++) {
			s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
		}
		s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
		s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
		s[8] = s[13] = s[18] = s[23] = "-";

		let uuid = s.join("");
		return uuid;
	},
	toLocalDate(time) {
		let date = new Date(time);
	
		let month = date.getMonth() + 1;
		if(month < 10) {
			month = '0' + month
		}
		let hours = date.getHours();
		if (hours < 10)
			hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
			minutes = "0" + minutes;
		let result = date.getFullYear() + "-" + month + "-" + date.getDate() ;
		return result;
	},
	//'2020-12-19T15:44:29+08:00' => '2020-12-19 15:44:29'
	toLocalTime(time) {
		let date = new Date(time);

		let month = date.getMonth() + 1;
		let hours = date.getHours();
		if (hours < 10)
			hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
			minutes = "0" + minutes;
		let result = date.getFullYear() + "-" + month + "-" + date.getDate() +
			" " + hours + ":" + minutes+ ":"+ date.getSeconds();
		return result;
	},
	downloadImage(id,callback){
		Vue.prototype.$http.post('common/file/download', {
			id: id
		}).then(res => {		
			if (res.code == 10000) {
				callback(res.data)
			}
		})
	}
}
export default funs
